package com.yxkj.syh.app.huarong.activities.creat.edit;

import android.app.Application;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.MutableLiveData;

import com.syh.app.basic.base.BaseResponse;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.bindingAdp.view_click.ClickEvent;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.utils.Tooast;
import com.syh.app.basic.utils.net.ApiObserver;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.yxkj.syh.app.huarong.bean.Goods;
import com.yxkj.syh.app.huarong.bean.OrderInfo;
import com.yxkj.syh.app.huarong.bean.Payment;
import com.yxkj.syh.app.huarong.bean.PaymentResponse;
import com.yxkj.syh.app.huarong.bean.ScanCarIdResponse;
import com.yxkj.syh.app.huarong.bean.UserMainInfo;
import com.yxkj.syh.app.huarong.data_center.model.OrderModel;
import com.yxkj.syh.app.huarong.data_center.model.PaymentModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建者：SYH
 * 时间：2020/5/28 17:38
 * 编辑者：SYH、
 * 标签：
 * 补充说明：
 */
public class EditOrderVM extends BaseViewModel<ActivityEvent> {
    OrderInfo orderInfo;

    public EditOrderVM(@NonNull Application application) {
        super(application);
    }

    public ObservableField<String> ofPayment = new ObservableField<>();
    public ObservableField<String> ofSupplier = new ObservableField<>();
    public ObservableField<String> ofBuyer = new ObservableField<>();
    public ObservableField<String> ofFactory = new ObservableField<>();
    public ObservableField<String> ofCarId = new ObservableField<>();
    public ObservableField<String> ofFromName = new ObservableField<>();
    public ObservableField<String> ofFromPhone = new ObservableField<>();
    public ObservableField<String> ofFromAddress = new ObservableField<>();
    public ObservableField<String> ofToName = new ObservableField<>();
    public ObservableField<String> ofToPhone = new ObservableField<>();
    public ObservableField<String> ofToAddress = new ObservableField<>();
    public ObservableField<String> ofRemark = new ObservableField<>();

    public MutableLiveData<Boolean> mldCreated = new MutableLiveData<>();

    public ClickEvent confirmClick = new ClickEvent() {
        @Override
        public void onClick(View view) {
            if (TextUtils.isEmpty(ofCarId.get())) {
                Tooast.normalInfo("请输入车牌号");
            } else {
                Map<String, Object> pMap = new HashMap<>();
                pMap.put("flag", 0);
                pMap.put("id", orderInfo.getId());
                pMap.put("message", ofRemark.get());
                Map<String, Object> carMap = new HashMap<>();
                carMap.put("license", ofCarId.get());
                pMap.put("params", carMap);

                showLoading("faHuo");
                OrderModel.getOrderModel().faHuo(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        hideLoading("faHuo");
                        Tooast.success(response.getMessage());
                        mldCreated.setValue(true);
                    }

                    @Override
                    public void onFailed(int code, String msg) {
                        hideLoading("faHuo");
                        Tooast.warning(msg);
                    }
                });
            }
        }
    };

    public void initOrderInfo(OrderInfo orderInfo) {
        this.orderInfo = orderInfo;
        getPaymentDetail(orderInfo.getBankId());
        setFromText(orderInfo.getStartLocation());
        setToText(orderInfo.getEndLocation());
        ofBuyer.set(orderInfo.getSalesman().getRealName());
        ofSupplier.set(orderInfo.getSupplierName());
        ofFactory.set(orderInfo.getEndLocation().getName());
    }

    public OrderInfo getOrderInfo() {
        return orderInfo;
    }


    public void setPaymentText(Payment payment) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("姓名：").append(payment.getAccountName()).append("\n");
        switch (payment.getBankName()) {
            case "网银":
                buffer.append("银行卡号：").append(payment.getCardNumber()).append("\n");
                buffer.append("开户行：").append(payment.getOpeningBank());
                break;
            case "支付宝":
                buffer.append("支付宝账户：").append(payment.getCardNumber());
                break;
            case "微信":
                buffer.append("微信账户：").append(payment.getCardNumber());
                break;
        }
        ofPayment.set(buffer.toString());
    }

    public void setFromText(UserMainInfo fromUserMainInfo) {
        StringBuffer buffer = new StringBuffer();
        ofFromName.set(fromUserMainInfo.getLinkman());
        ofFromPhone.set(fromUserMainInfo.getMobile());
        ofFromAddress.set("发货地址：" + fromUserMainInfo.getArea().replace("/","") + fromUserMainInfo.getAddress());
    }

    public void setToText(UserMainInfo toUserMainInfo) {

        ofToName.set(toUserMainInfo.getLinkman());
        ofToPhone.set(toUserMainInfo.getMobile());
        ofToAddress.set("收货地址：" + toUserMainInfo.getArea().replace("/","") + toUserMainInfo.getAddress());

    }

    public void editOrderPayment(String bankID) {
        List<Goods> goodsListP = orderInfo.getDetails();
        Map<String, Object> pMap = new HashMap<>();
        List<Map<String, Object>> goodsList = new ArrayList<>();
        for (Goods goods : goodsListP) {
            Map<String, Object> goodsMap = new HashMap<>();
            goodsMap.put("quantity", goods.getQuantity());
            goodsMap.put("packages", goods.getPackages());
            goodsMap.put("productNo", goods.getProductNo());
            goodsMap.put("productId", goods.getProductId());
            goodsMap.put("name", goods.getName());
            goodsList.add(goodsMap);
        }
        pMap.put("details", goodsList);
        pMap.put("bankId", bankID);
        pMap.put("id", orderInfo.getId());

        showLoading("editGoodsOrPayment");
        OrderModel.getOrderModel().editGoodsOrPayment(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                hideLoading("editGoodsOrPayment");
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("editGoodsOrPayment");
                Tooast.warning(msg);
            }
        });
    }

    public void scanCarId(String path) {
        showLoading("scanCarId");
        String base64 = "data:image/jpg;base64," + ComUtil.imageToBase64(path);
        Map<String, Object> pMap = new HashMap<>();
        pMap.put("base64Image", base64);
        OrderModel.getOrderModel().scanCarId(pMap, mLifecycleProvider.bindToLifecycle(), new ApiObserver<ScanCarIdResponse>() {
            @Override
            public void onSuccess(ScanCarIdResponse scanCarIdResponse) {
                hideLoading("scanCarId");
                ofCarId.set(scanCarIdResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                hideLoading("scanCarId");
                Tooast.warning(msg);
            }
        });
    }

    private void getPaymentDetail(long id) {
        PaymentModel.getPaymentModel().paymentDetail(id, mLifecycleProvider.bindToLifecycle(), new ApiObserver<PaymentResponse>() {
            @Override
            public void onSuccess(PaymentResponse paymentResponse) {
                orderInfo.setPayment(paymentResponse.getData());
                setPaymentText(paymentResponse.getData());
            }

            @Override
            public void onFailed(int code, String msg) {
                Tooast.warning(msg);
            }
        });
    }

}
